<%--
  Created by IntelliJ IDEA.
  User: 风间琉璃
  Date: 2021/6/22
  Time: 17:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="RentServlet?opr=byUserName" method="post">
    <input type="text" name="userName" placeholder="租赁人姓名">
    <button type="submit">go</button>
  </form>
  <h1>停车场车位租赁管理系统</h1>
  <button type="button" id="add">车位租赁</button>
    <table>
      <tr style="background: #666666">
        <th>租赁人姓名</th>
        <th>车牌号</th>
        <th>租赁状态</th>
        <th>到期日期</th>
        <th>操作</th>
      </tr>
      <c:forEach items="${list}" var="r" varStatus="s">

        <tr <c:if test="${s.index % 2 == 1}">style="background:#888888";</c:if>>
          <td>${r.userName}</td>
          <td>${r.carNum}</td>
          <td>${r.status  == 0 ? '租赁中' : '已到期'}</td>
          <td>${r.maturityDate}</td>
          <td status="${r.status}" id="${r.id}">
            <a class="statusChange" href="javascript:void(0)">${r.status  == 0 ? '退租' : '已到期'}</a>
            <a href="javascript:void(0)" class="del" rid="${r.id}">删除</a>
          </td>
        </tr>
      </c:forEach>
      <c:if test="${pageIndex > 1}">
        <a href="RentServlet?opr=list&pageIndex=${pageIndex - 1}">上一页</a>
      </c:if>
      当前页码${pageIndex}/总页码${pageCount}
      <c:if test="${pageIndex < pageCount}">
        <a href="RentServlet?opr=list&pageIndex=${pageIndex + 1}">下一页</a>
      </c:if>
    </table>
    <span id="span" style="display: none">退租成功</span>
  <script src="js/jquery-1.8.3.js"></script>
  <script>
    $(document).ready(function (){
      $(".statusChange").click(function (){
        var node = $(this);
        var statusNode = $(this).parent().prev().prev();
        var status = $(this).parent().attr("status");
        var id = $(this).parent().attr("id");
        var span = $("span");
        // alert(id);
        if (status == 0){
          $.post("RentServlet?opr=upd",{"id":id,"status":1},function (data){
            if (data == "退租成功"){
              node.text("已到期");
              statusNode.text("已到期");

              span.show();
              setTimeout(function (){
                span.hide();
              },5000);
            }
          });
        }
      }),
      $(".del").click(function (){
        var node = $(this).parent().parent();
        var id = $(this).attr("rid");
        var b = confirm("确认要删除嘛？");
        if (b == true){
          $.post("RentServlet?opr=delete","id="+id,function (data){
            alert(data);
            if (data == "删除成功")
              node.remove();
          })
        }
      })
      $("#add").click(function (){
        location.href="add.jsp";
      })
    })
  </script>
  </body>
</html>
